package com.jfour.misc.code;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.jfour.misc.code.Column;
import com.jfour.misc.code.Table;

public class PdmParser {

	/**
	 * 解析xml文档，将解析到得数据封装到Table对象当中
	 */
	
	public List<Table> getXmlTable(String xmlPath){
		
		List <Table> tableList = new ArrayList<Table>();
		
		//根据xml文件，将文件已对象的形式表现出来
		File file = new File(xmlPath);
		//构建解析方式
		SAXReader reader = new SAXReader();
		//得到文档对象
		Document doc = null;
		try {
			doc = reader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.out.println("文档读取失败！");
			return null;
		}
		//根据xpath方式得到所要得到xml文档的具体对象,根据分析解析xml文档可知，xml文档中含有前缀名
		Map<String,String> map = new HashMap<String, String>();
		map.put("c","collection");
		//根据xml文档，//c:Table 即为得到的文档对象
		XPath path = doc.createXPath("//c:Tables");
		path.setNamespaceURIs(map);
		List<Element> list = path.selectNodes(doc);
		//得到tables对象，该对象是该pdm文件中所有表的集合
		for(Element tables : list){
			for(Iterator<Element> iter = tables.elementIterator("Table");iter.hasNext();){
				Element tableElement = iter.next();
				Table table = new Table();
				table.setCode(tableElement.elementText("Code"));
				table.setName(tableElement.elementText("Name"));
				table.setComment(tableElement.elementText("Comment"));
				//获取table主键
				String primaryColumnId = null;
				Element primaryKeyElement = tableElement.element("PrimaryKey");
				if(primaryKeyElement != null){
					String keyId = primaryKeyElement.element("Key").attributeValue("Ref");
					Attribute primaryColumnIdAttribute = (Attribute)tableElement.selectSingleNode("c:Keys/o:Key[@Id=\"" + keyId + "\"]/c:Key.Columns/o:Column/@Ref");
					if(primaryColumnIdAttribute != null){
						primaryColumnId = primaryColumnIdAttribute.getValue();
					}
				}
				//通过一个table对象，得到columns对象
				Element columns = tableElement.element("Columns");
				List <Column> columnList = new ArrayList<Column>();
				for(Iterator<Element> cols = columns.elementIterator("Column");cols.hasNext();){
					Element columnElement = cols.next();
					Column column = new Column();
					column.setCode(columnElement.elementText("Code"));
					column.setName(columnElement.elementText("Name"));
					column.setDataType(columnElement.elementText("DataType"));
					column.setLength(columnElement.elementText("Length") == null ? 0 : Integer.valueOf(columnElement.elementText("Length")));
					columnList.add(column);
					if(primaryColumnId != null && primaryColumnId.equals(columnElement.attributeValue("Id"))){
						column.setPrimaryKey(true);
					}
				}
				table.setColumnList(columnList);
				tableList.add(table);
			}
		}
		
		return tableList;
	}
	
	
	public static void main(String [] args){
		PdmParser xp = new PdmParser();
		List<Table> tables = xp.getXmlTable("E:/dev/myworkspace/platform/sql/WorkFlow.pdm");
		for (Table table : tables) {
			System.out.println(table.getCode() + "	:	" + table.getName());
			for (Column column : table.getColumnList()) {
				StringBuilder sb = new StringBuilder();
				sb.append("	" + column.getCode() + "	:	" + column.getName());
				sb.append("	" +column.getDataType() + "(" + column.getLength() + ")");
				if(column.isPrimaryKey()) {
					sb.append("		pk");
				}
				System.out.println(sb.toString());
			}
		}
	}
}

